#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 时间     : 2021-04-27 17:53
# 作者     : DL (584054432@qq.com)
# 网址     : https://gitee.com/dingliangPython/
# 软件版本 : Python3.7.3
# 功能     ：

import logging
import os

import loadConfig
from PyQt5.QtCore import pyqtSignal, pyqtSlot
from PyQt5.QtWidgets import QDialog
from pyqt5_common import APP_CONFIG_PATH, APP_NAME, error_info, show_message

from sdk_config_ui import Ui_Form


class Window(QDialog):
    """docstring for Window"""
    sdk = None
    sdk_start_signal = pyqtSignal(bool)

    def __init__(self, start = None):
        super(Window, self).__init__()
        self.start = start
        self.log = logging.getLogger('main.sdk_config')
        self.config_path = os.path.join(APP_CONFIG_PATH, APP_NAME, 'gcl_sdk/config/apps.config.ini')
        self.config_obj = loadConfig.loadIni(self.config_path, 'gbk')
        self.ui = Ui_Form()
        self.ui.setupUi(self)
        self.load_config()
        self.sdk_start_signal.connect(lambda v: self.load_config(v) if v else None)

    def closeEvent(self, event):
        """
        重写closeEvent方法，实现dialog窗体关闭时执行一些代码
        :param event: close()触发的事件
        :return: None
        """
        result = show_message("配置改变重启程序才能生效，是否退出程序")
        if result:
            os._exit(0)
        else:
            self.hide()

    # 加载配置
    def load_config(self,start=None):
        if start:
            self.start=start
        if not self.start:
            return
        if not os.path.isfile(self.config_path):
            show_message("加载sdk配置文件:{},失败,请检测是否存在配置文件...".format(self.config_path))
            return
        self.ui.sdk_log_switch.setCheckState(0 if self.config_obj.ini['ProgramConfigs']['LogDisables'] == 'all' else 2)
        self.ui.sdk_local_port.setText(str(self.config_obj.ini['GeneralAccess']['LocalPort']))
        self.ui.sdk_local_host.setText(self.config_obj.ini['GeneralAccess']['LocalIpAddress'])
        self.ui.sdk_remote_host.setText(self.config_obj.ini['GeneralAccess']['RemoteIpAddress'])
        self.ui.sdk_remote_port.setText(str(self.config_obj.ini['GeneralAccess']['RemotePort']))

        yx = self.ui.sdk_yaoxin_begin
        yx.setText(self.config_obj.ini['Measure']['MonsbBegin'])
        self.ui.sdk_yaoxin_hex.setText(str(self.to_number(yx.text())))
        yx.editingFinished.connect(lambda: self.ui.sdk_yaoxin_hex.setText(str(self.to_number(yx.text()))))
        self.ui.sdk_yaoxin_count.setText(str(self.config_obj.ini['Measure']['MonsbCount']))

        yc = self.ui.sdk_yaoce_begin
        yc.setText(self.config_obj.ini['Measure']['YcaddBegin'])
        self.ui.sdk_yaoce_hex.setText(str(self.to_number(yc.text())))
        yc.editingFinished.connect(lambda: self.ui.sdk_yaoce_hex.setText(str(self.to_number(yc.text()))))
        self.ui.sdk_yaoce_count.setText(str(self.config_obj.ini['Measure']['YcaddCount']))

        yw = self.ui.sdk_yaowen_begin
        yw.setText(self.config_obj.ini['Measure']['StrawBegin'])
        self.ui.sdk_yaowen_hex.setText(str(self.to_number(yw.text())))
        yw.editingFinished.connect(lambda: self.ui.sdk_yaowen_hex.setText(str(self.to_number(yw.text()))))
        self.ui.sdk_yaowen_count.setText(str(self.config_obj.ini['Measure']['StrawCount']))

    @error_info
    def to_number(self, value):
        if not value:
            return
        return int(value, 16)

    # SDK TID修改
    def on_sdk_local_id_editingFinished(self):
        value = self.ui.sdk_local_id.text()
        self.config_obj.ini['GeneralAccess']['PsmSentSourceId'] = value
        self.log.info("修改sdk,终端ID:{}".format(value))
        self.config_obj.write()

    # SDK 本地端口修改
    def on_sdk_local_port_editingFinished(self):
        value = self.ui.sdk_local_port.text()
        self.config_obj.ini['GeneralAccess']['LocalPort'] = value
        self.log.info("修改sdk,终端ID:{}".format(value))
        self.config_obj.write()

    # SDK 远程主机修改
    def on_sdk_remote_host_editingFinished(self):
        value = self.ui.sdk_remote_host.text()
        self.config_obj.ini['GeneralAccess']['RemoteIpAddress'] = value
        self.log.info("修改sdk,终端ID:{}".format(value))
        self.config_obj.write()

    # SDK 日志启停
    @pyqtSlot(int)
    def on_sdk_log_switch_stateChanged(self, value):
        value = 'all' if not value else ''
        self.config_obj.ini['ProgramConfigs']['LogDisables'] = value
        self.log.info("修改sdk,日志启动:{}".format(value))
        self.config_obj.write()

    # 遥信地址开始修改
    def on_sdk_yaoxin_begin_editingFinished(self):
        value = self.ui.sdk_yaoxin_begin.text()
        self.config_obj.ini['Measure']['MonsbBegin'] = value
        self.log.info("修改sdk,遥信开始地址:{}".format(value))
        self.config_obj.write()

    # 遥信地址计数
    def on_sdk_yaoxin_count_editingFinished(self):
        value = self.ui.sdk_yaoxin_count.text()
        self.config_obj.ini['Measure']['MonsbCount'] = value
        self.log.info("修改sdk,遥信地址计数:{}".format(value))
        self.config_obj.write()

    # 遥测地址开始修改
    def on_sdk_yaoce_begin_editingFinished(self):
        value = self.ui.sdk_yaoce_begin.text()
        self.config_obj.ini['Measure']['YcaddBegin'] = value
        self.log.info("修改sdk,遥测开始地址:{}".format(value))
        self.config_obj.write()

    # 遥测地址计数
    def on_sdk_yaoce_count_editingFinished(self):
        value = self.ui.sdk_yaoce_count.text()
        self.config_obj.ini['Measure']['YcaddCount'] = value
        self.log.info("修改sdk,遥测地址计数:{}".format(value))
        self.config_obj.write()

    # 遥文地址开始修改
    def on_sdk_yaowen_begin_editingFinished(self):
        value = self.ui.sdk_yaowen_begin.text()
        self.config_obj.ini['Measure']['StrawBegin'] = value
        self.log.info("修改sdk,遥文开始地址:{}".format(value))
        self.config_obj.write()

    # 遥文地址计数
    def on_sdk_yaowen_count_editingFinished(self):
        value = self.ui.sdk_yaowen_count.text()
        self.config_obj.ini['Measure']['StrawCount'] = value
        self.log.info("修改sdk,遥文地址计数:{}".format(value))
        self.config_obj.write()